﻿#include "mainwindow.h"


MainWindow::MainWindow(QWidget *parent)
    : QWidget(parent)
{
    setup_ui();
    connect(treeView, &QAbstractItemView::clicked, this, &MainWindow::slt_treeViewClick);
}

MainWindow::~MainWindow()
{
}

//uis
void MainWindow::setup_ui()
{
    qApp->setStyleSheet(read_style(":/qss/rc/main.qss"));
    //set_up_dtu_ui();
    QGridLayout* headContainer = setup_head();
    QGridLayout* treeViewContainer = setup_treeview();

    mainStackWidget = new QStackedWidget();
    dtuWindow = new DTUWindow();
    injectorWindow = new InjectorWindow();
    mainStackWidget->addWidget(dtuWindow);
    mainStackWidget->addWidget(injectorWindow);

    // 0 dtu
    // 1 injector
    mainStackWidget->setCurrentIndex(0);

    setGeometry(QRect(90, 40, 1600, 900));
    int width = 64, height = 18;
    int head_h = 1;
    int tree_w = 6;
    QGridLayout *mainLayout = new QGridLayout(this);
    mainLayout->addLayout(headContainer, 0, 0, head_h, width, 0);
    mainLayout->addLayout(treeViewContainer, head_h, 0,  height - head_h, tree_w);
    mainLayout->addWidget(mainStackWidget, head_h, tree_w,  height - head_h, width - tree_w);
    mainLayout->setMargin(0);
    mainLayout->setSpacing(1);
    this->setWindowTitle(QStringLiteral("ZPMC岸桥物联网平台"));
    setLayout(mainLayout);
    //setWindowFlags(Qt::CustomizeWindowHint );//| Qt::WindowCloseButtonHint
}

QGridLayout* MainWindow::setup_head()
{
    QGridLayout* layout = new QGridLayout();

    QLabel* headpic = new QLabel("head");
    headpic->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);
    QImage *img = new QImage();
    img->load(":/pics/rc/logo_dark_small.png");
    headpic->setStyleSheet("background-color:#109fd9");
    headpic->setPixmap(QPixmap::fromImage(*img));
    layout->addWidget(headpic);
    return layout;
}

QGridLayout* MainWindow::setup_treeview()
{
    QGridLayout* layout = new QGridLayout();
    treeView = new QTreeView();
    treeView->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);
    //temps for visual
    QStandardItemModel* model = setup_treemodel();

    treeView->setHeaderHidden(true);
    treeView->setModel(model);

    treeView->setAnimated(true);
    treeView->setIndentation(20);
    treeView->setSortingEnabled(false);
    treeView->setStyleSheet(read_style(":/qss/rc/treeview.qss"));
    //treeView->setStyleSheet("font: 18px \"Microsoft YaHei\";");

    layout->addWidget(treeView);
    return layout;
}

QStandardItemModel* MainWindow::setup_treemodel()
{
    QStandardItemModel* model = new QStandardItemModel(treeView);

    QStandardItem* injector = new QStandardItem(QStringLiteral("注脂机"));
    QStandardItem* dtu = new QStandardItem(QStringLiteral("DTU"));
    model->appendRow(injector);
    model->appendRow(dtu);

    //level 2 position
    QStandardItem* item11 = new QStandardItem(QStringLiteral("position"));
    injector->appendRow(item11);
    QStandardItem* item21 = new QStandardItem(QStringLiteral("position"));
    dtu->appendRow(item21);

    return model;
}

void MainWindow::slt_treeViewClick(const QModelIndex& index)
{
    if(index.parent().isValid())
    {
        if(index.parent().data().toString() == QStringLiteral("注脂机"))
            mainStackWidget->setCurrentIndex(1);

        else
            mainStackWidget->setCurrentIndex(0);
    }
    //qDebug()<<index.parent().data().toString();
}


